package com.foreveross.chameleon.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

import com.foreveross.chameleon.config.ChameleonConfig;
import com.foreveross.chameleon.model.ApplicationInfo;
import com.foreveross.chameleon.task.AppDownloadCountUpdateTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

public class DownloadUpdateActivity extends Activity {

	public static final String INTENT_CURRENT_APPLICATION = "com.foreveross.cube.appnew";
	public static final String INTENT_NEW_APPLICATION = "com.foreveross.cube.appcurrent";

	DownloadTask downloadTask;
	ProgressDialog progressDialog;
	WakeLock wakeLock;

	int newBuild = 0;
	String appKey = "";
	ApplicationInfo newApp;
	ApplicationInfo curApp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 透明activity
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().getDecorView().setBackgroundColor(Color.WHITE);
		setContentView(new View(this));

		// 创建进度对话框
		progressDialog = new ProgressDialog(this);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progressDialog.setMax(100);
		progressDialog.setTitle("版本更新");
		progressDialog.setMessage("下载中，请稍候...");
		progressDialog.setCanceledOnTouchOutside(false);
		progressDialog.setCancelable(false);
		progressDialog.setButton(Dialog.BUTTON_NEGATIVE, "取消",
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (downloadTask != null) {
							downloadTask.cancel(true);
						}
						Toast.makeText(DownloadUpdateActivity.this, "更新已取消",
								3 * 1000).show();

						DownloadUpdateActivity.this.finish();
					}
				});
		progressDialog.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_SEARCH
						&& event.getRepeatCount() == 0) {
					return true; // Pretend we processed it
				}
				return false; // Any other keys are still processed as normal
			}
		});

		newApp = (ApplicationInfo) getIntent().getExtras().getSerializable(
				INTENT_NEW_APPLICATION);
		curApp = (ApplicationInfo) getIntent().getExtras().getSerializable(
				INTENT_CURRENT_APPLICATION);
		// 开始下载
		newBuild = newApp.getBuild();
		appKey = ChameleonConfig.APP_ID;

		if (newApp.getDownloadUrl() != null && newApp.getDownloadUrl() != "") {
			downloadTask = new DownloadTask();
			downloadTask.execute(newApp);
		} else {
			Toast.makeText(DownloadUpdateActivity.this, "App 升级地址有误!", 3 * 1000)
					.show();
			DownloadUpdateActivity.this.finish();
		}

	}

	@Override
	protected void onStart() {
		super.onStart();

		// 下载期间，暗屏，不睡眠
		PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
		wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, this
				.getClass().getName());
		wakeLock.acquire();
	}

	@Override
	protected void onStop() {
		super.onStop();

		if (wakeLock != null && wakeLock.isHeld()) {
			wakeLock.release();
			wakeLock = null;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (downloadTask != null && !downloadTask.isCancelled()) {
			downloadTask.cancel(true);
		}

	}

	/*
	 * 获取存放更新安装包的路径
	 */
	public File getAppDataDir() {
		File appBasePath = null;
		File downloadCacheDirectory = Environment.getExternalStorageDirectory();
		if (downloadCacheDirectory != null) {
			appBasePath = new File(downloadCacheDirectory.getAbsolutePath()
					+ File.separator + "Android" + File.separator + "data"
					+ File.separator + ChameleonConfig.APP_PACKAGENAME
					+ File.separator + "files");

			if (!appBasePath.exists()) {
				appBasePath.mkdirs();
			}
		}
		return appBasePath;
	}

	/*
	 * 根据版本获取安装包文件
	 */
	public File getAPKFile(ApplicationInfo app) {
		String apkPath = String.format(getAppDataDir().getAbsolutePath()
				+ File.separator + "amp-%s.apk", app.getBuild());
		File apkFile = new File(apkPath);
		return apkFile;
	}

	public void install(ApplicationInfo app) {
		Log.d("VersionUpdate", "安装更新");

		// 更新应用信息
		updateAppinfo(curApp, newApp);
		Intent intent = new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(getAPKFile(app)),
				"application/vnd.android.package-archive");
		startActivity(intent);

		// 关闭自身，否则取消安装后，跳回此activity，会重新下载
		this.finish();
	}

	void showDownloadError(String error) {
		Log.d("VersionUpdate", "显示更新下载错误");

		// 关闭进度框
		progressDialog.dismiss();

		// 提示更新失败
		AlertDialog dialog = new AlertDialog.Builder(this).setTitle("版本更新出错")
				.setMessage(error)
				.setPositiveButton("确定", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						DownloadUpdateActivity.this.finish();
					}
				}).create();
		dialog.show();
	}

	class DownloadTask extends AsyncTask<ApplicationInfo, Integer, Void> {

		private ApplicationInfo newApp = null;
		private Throwable error = null;

		@Override
		protected void onPreExecute() {
			Log.d("VersionUpdate", "开始下载更新包");

			progressDialog.show();
			progressDialog.setProgress(0);
		}

		@Override
		protected void onProgressUpdate(Integer... progress) {
			// Log.d("VersionUpdate", "进度:" + progress[0]);
			progressDialog.setProgress(progress[0]);
		}

		@Override
		protected void onPostExecute(Void v) {
			Log.d("VersionUpdate", "下载完毕");

			progressDialog.dismiss();

			if (error != null) {
				showDownloadError(error.getMessage());
			} else {
				// 安装
				install(newApp);
			}
		}

		@Override
		protected void onCancelled() {
			progressDialog.dismiss();
		}

		@SuppressWarnings("deprecation")
		@Override
		protected Void doInBackground(ApplicationInfo... params) {
			System.out.println(params[0]);
			final ApplicationInfo app = params[0];
			newApp = app;
			final BasicHttpParams httpParameters = new BasicHttpParams();
			HttpConnectionParams
					.setConnectionTimeout(httpParameters, 15 * 1000);
			final HttpClient client = new DefaultHttpClient(httpParameters);
			String updateUrl = newApp.getDownloadUrl();
			final HttpGet get = new HttpGet(updateUrl);
			System.out.println("更新包地址为:url =" + get.getURI());
			get.setHeader("User-Agent", "Deamon");

			InputStream is = null;
			FileOutputStream fos = null;

			File apkFile = getAPKFile(app);
			try {
				HttpResponse response = client.execute(get);
				if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

					final HttpEntity entity = response.getEntity();
					final long length = entity.getContentLength();
					Log.d("VersionUpdate", "安装包大小:" + length / 1000 + "kb");
					is = entity.getContent();

					if (!apkFile.exists())
						apkFile.createNewFile();
					fos = new FileOutputStream(apkFile);

					byte[] buffer = new byte[1024 * 100];
					int count = 0;
					long total = 0;
					while ((count = is.read(buffer, 0, buffer.length)) != -1) {
						fos.write(buffer, 0, count);
						total += count;
						long progress = total * 100 / length;
						publishProgress((int) progress);// 通知进度更新
					}
					Log.d("VersionUpdate", "实际大小:" + total / 1000 + "kb");
					if (total != length) {
						throw new RuntimeException(
								"安装包大小与服务器不符，可能由于网络不稳定导致，请重新更新");
					}
				} else {
					throw new RuntimeException("安装包下载失败，服务器连接异常，状态码:"
							+ response.getStatusLine().getStatusCode());
				}
			} catch (Throwable e) {
				Log.e("VersionUpdate", "获取更新失败", e);
				error = new Exception("访问发生错误，请检查网络");

			} finally {
				try {
					if (fos != null) {
						fos.flush();
						fos.close();
					}
					if (is != null) {
						is.close();
					}
				} catch (IOException e) {
				}
			}

			return null;
		}

	}

	private void updateAppinfo(ApplicationInfo app, ApplicationInfo newApp) {
		if (newApp.getAppKey() != null) {
			app.setAppKey(newApp.getAppKey());
		}
		if (newApp.getBuild() != app.getBuild()) {
			app.setBuild(newApp.getBuild());
		}
		if (newApp.getDownloadUrl() != app.getDownloadUrl()) {
			app.setDownloadUrl(newApp.getDownloadUrl());
		}
		if (newApp.getIcon() != app.getIcon()) {
			app.setIcon(newApp.getIcon());
		}
		if (newApp.getName() != app.getName()) {
			app.setName(newApp.getName());
		}
		if (newApp.getVersion() != app.getVersion()) {
			app.setVersion(newApp.getVersion());
		}

		app.save(app);
	}

}
